/*
 * JDialogPropiedadesComponente.java
 *
 * Copyright 2011 Universidad Francisco de Paula Santander (UFPS).
 * Todos los Derechos Reservados.
 * Created on 24/01/2011, 06:13:44 PM
 */

package com.ufps.cuatro.vistas.representacion;

import java.util.Observable;
import java.util.Observer;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;

import org.jdesktop.application.Action;

import com.ufps.cuatro.CuatroApp;
import com.ufps.cuatro.controlador.listener.ControllerListenerPopupJTableProperties;
import com.ufps.cuatro.controlador.render.UtilRender;
import com.ufps.cuatro.modelo.asistente.UtilAsistente;
import com.ufps.cuatro.modelo.representacion.Propiedad;
import com.ufps.cuatro.modelo.representacion.componente.Componente;
import com.ufps.cuatro.util.Bandera;
import com.ufps.cuatro.util.Utileria;
import com.ufps.cuatro.util.editor.ColumnCellEditorPropertiesDisponibleColumnCodeGet;
import com.ufps.cuatro.util.editor.ColumnCellEditorPropertiesDisponibleColumnCodeSet;
import com.ufps.cuatro.util.editor.ColumnCellEditorPropertiesDisponibleColumnValue;
import com.ufps.cuatro.util.model.MyTableModelProperties;
import com.ufps.cuatro.util.render.ColumnCellRendererPropertiesDisponibleColumnCodeGet;
import com.ufps.cuatro.util.render.ColumnCellRendererPropertiesDisponibleColumnCodeSet;
import com.ufps.cuatro.util.render.ColumnCellRendererPropertiesDisponibleColumnValues;

/**
 *<i><b>JDialogPropiedadesComponente:</b></i> Este Cuadro de dialog Administra
 * las Propiedades de Configuración de los Controles Swing que son representados
 * por los Componentes Generados.
 * 
 * @author Carlos A. Torres C. - Dolly E. Guerrero. <https://code.google.com/p/tesis4dor/>
 * @version 1.0 24/01/2011
 * @see javax.swing.JDialog
 * @see Observer
 */
public class JDialogPropiedadesComponente extends javax.swing.JDialog implements Observer{
    private static final long serialVersionUID = -374176212040399568L;
    
    private Componente objComponente;
    private Object objParent;

    /** 
     * Creates new form JDialogPropiedadesComponente
     * @param parent Referecnia del Formulario Padre.
     * @param modal  Estado Modal.
     */
    public JDialogPropiedadesComponente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setIconImage(Utileria.leerIconoAppEnImagen(Utileria.MODO_16x16).getImage());
    }

    /**
     * Creates new form JDialogPropiedadesComponente
     * @param objParent Referecnia del Objeto Padre-
     * @param objComponente Referecnia del Componente al que se le Adminsitrarán
     * las Propiedades Disponibles.
     */
    public JDialogPropiedadesComponente(Object objParent, Componente objComponente) {
        super(CuatroApp.getApplication().getMainFrame() , true);
        initComponents();
        this.setIconImage(Utileria.leerIconoAppEnImagen(Utileria.MODO_16x16).getImage());
        this.objParent = objParent;        
        this.cargarPropiedades(objComponente);
    }
    
    private void cargarPropiedades(Componente objComponente){        
        this.objComponente = objComponente;
        this.jTextFieldClassPath.setText(objComponente.getStrClassPath());
        this.jLabelComponenteContenedor.setText(objComponente.getObjComponenteBase().getStrAlias());        
        crearTablaPropiedadesDisponibles(objComponente);
    }   

    private void crearTablaPropiedadesDisponibles(Componente objComponente) {
        this.jTablePropiedadesUsuario.setModel(UtilAsistente.crearTablaPropiedadesDisponibles(objComponente.getObjPropiedadesDisponibles()));
        this.jTablePropiedadesUsuario.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        
        TableColumn objColumnValor = this.jTablePropiedadesUsuario.getColumn(MyTableModelProperties.NAME_COLUMN_VALUES);
        objColumnValor.setCellRenderer(new ColumnCellRendererPropertiesDisponibleColumnValues(objComponente));
        objColumnValor.setCellEditor(new ColumnCellEditorPropertiesDisponibleColumnValue(objComponente));                        
                
        TableColumn objColumnSet = this.jTablePropiedadesUsuario.getColumn(MyTableModelProperties.NAME_COLUMN_VIEWSET);
        objColumnSet.setCellRenderer(new ColumnCellRendererPropertiesDisponibleColumnCodeSet());
        objColumnSet.setCellEditor(new ColumnCellEditorPropertiesDisponibleColumnCodeSet());                
        
        TableColumn objColumnGet = this.jTablePropiedadesUsuario.getColumn(MyTableModelProperties.NAME_COLUMN_VIEWGET);
        objColumnGet.setCellRenderer(new ColumnCellRendererPropertiesDisponibleColumnCodeGet());
        objColumnGet.setCellEditor(new ColumnCellEditorPropertiesDisponibleColumnCodeGet());
        
        this.jTablePropiedadesUsuario.setRowHeight(24);        
        
        this.jTablePropiedadesUsuario.getColumnModel().getSelectionModel().addListSelectionListener(new ColumnListener());                
    }
    
    private void createPopupMenu() {           
        if(this.jTablePropiedadesUsuario.getSelectedColumn() == 1){
            this.jTablePropiedadesUsuario.removeMouseListener(null);
            int row = this.jTablePropiedadesUsuario.getSelectedRow();
            int col = this.jTablePropiedadesUsuario.getSelectedColumn();
            Object objValue = this.jTablePropiedadesUsuario.getValueAt(row, col);
            if(objValue instanceof Propiedad){
                Propiedad objPropiedad = (Propiedad) objValue;
                if(objPropiedad.isEmpty()){
                    ControllerListenerPopupJTableProperties objMyPopup = new ControllerListenerPopupJTableProperties(this, jTablePropiedadesUsuario, objValue);
                    this.jTablePropiedadesUsuario.addMouseListener(objMyPopup);        
                }
                else{
                    UtilRender.msgAdvertencia("La Propiedad No Puede ser eliminada.");
                    objPropiedad.getObjParametrosSet().get(0).setStrValue(objPropiedad.strValueTempParametro);
                    this.jTablePropiedadesUsuario.repaint();
                }                
            }
            
        }        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton2 = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jButton3 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabelComponenteContenedor = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldClassPath = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTablePropiedadesUsuario = new javax.swing.JTable();
        jLabelPanelTitle = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(JDialogPropiedadesComponente.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setIconImage(null);
        setName("Form"); // NOI18N
        setResizable(false);

        jToolBar1.setFloatable(false);
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(JDialogPropiedadesComponente.class, this);
        jButton1.setAction(actionMap.get("aplicarCambios")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        jButton2.setAction(actionMap.get("cancelarCambios")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton2);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        jButton3.setAction(actionMap.get("help")); // NOI18N
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton3);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabelComponenteContenedor.setBackground(resourceMap.getColor("jLabelComponenteContenedor.background")); // NOI18N
        jLabelComponenteContenedor.setText(resourceMap.getString("jLabelComponenteContenedor.text")); // NOI18N
        jLabelComponenteContenedor.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelComponenteContenedor.setName("jLabelComponenteContenedor"); // NOI18N
        jLabelComponenteContenedor.setOpaque(true);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jTextFieldClassPath.setEditable(false);
        jTextFieldClassPath.setText(resourceMap.getString("jTextFieldClassPath.text")); // NOI18N
        jTextFieldClassPath.setName("jTextFieldClassPath"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTablePropiedadesUsuario.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jTablePropiedadesUsuario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Propiedad", "Valor", "..."
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTablePropiedadesUsuario.setToolTipText(resourceMap.getString("jTablePropiedadesUsuario.toolTipText")); // NOI18N
        jTablePropiedadesUsuario.setCellSelectionEnabled(true);
        jTablePropiedadesUsuario.setName("jTablePropiedadesUsuario"); // NOI18N
        jTablePropiedadesUsuario.setRowHeight(24);
        jTablePropiedadesUsuario.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jTablePropiedadesUsuario);

        jLabelPanelTitle.setBackground(resourceMap.getColor("jLabelPanelTitle.background")); // NOI18N
        jLabelPanelTitle.setIcon(resourceMap.getIcon("jLabelPanelTitle.icon")); // NOI18N
        jLabelPanelTitle.setText(resourceMap.getString("jLabelPanelTitle.text")); // NOI18N
        jLabelPanelTitle.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelPanelTitle.setName("jLabelPanelTitle"); // NOI18N
        jLabelPanelTitle.setOpaque(true);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelComponenteContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                            .addComponent(jTextFieldClassPath, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jLabelPanelTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
                .addGap(57, 57, 57))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel3, jLabel4});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabelPanelTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelComponenteContenedor)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextFieldClassPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE))
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JDialogPropiedadesComponente dialog = new JDialogPropiedadesComponente(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    @Action
    public void aplicarCambios() {
        Bandera objBandera = new Bandera();
        objBandera.addObserver((Observer) objParent);
        objBandera.setEstado(Bandera.PROPIEDADES_ACTUALIZADAS);                
        objBandera.notificar();  
        dispose();
    }

    @Action
    public void cancelarCambios() {
        String strMsg = Utileria.leerRecursoTexto("Utileria.msg.salir.window");
        String strTitle = Utileria.leerRecursoTexto("Utileria.app.name");        
        if(Utileria.confirmarOrden(CuatroApp.getApplication().getMainFrame(), strMsg, strTitle)){
            dispose();
        }
    }
    
    public void update(Observable o, Object arg) {
        try{
            if(o instanceof Bandera){                
                Bandera objBandera = (Bandera) o;
                if(objBandera.getEstado() == Bandera.VALUE_RESET_PROPERTIES){
                    this.jTablePropiedadesUsuario.repaint();
                }                
            }       
        }catch(java.lang.NullPointerException ex){  
            UtilRender.printlnConsole(ex.getMessage(), UtilRender.ACTION_ERROR, true);
        }
    }

    @Action
    public void help() {
        UtilRender.openDialogHelp(JDialogPropiedadesComponente.class, getTitle());
    }
    
    private class ColumnListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent event) {
            if (event.getValueIsAdjusting()) {
                return;
            }
            createPopupMenu();
        }
    }    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelComponenteContenedor;
    private javax.swing.JLabel jLabelPanelTitle;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JTable jTablePropiedadesUsuario;
    private javax.swing.JTextField jTextFieldClassPath;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}